package com.lyn.dwjhb.convertor;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.lyn.dwjhb.model.SysDept;
import com.lyn.dwjhb.model.TblDept;



public class DeptConvertor extends BaseConvertor{
	
	public DeptConvertor(QueryRunner rq) {
		super(rq);
	}

	//把dwjhb的部门对象转为dcasp的部门对象
	public SysDept convert(TblDept tblDept) {
		SysDept sysDept = new SysDept();
		
		sysDept.setGuid(tblDept.getId());
		sysDept.setGmtCreate(tblDept.getXgsj());
		sysDept.setGmtModified(tblDept.getXgsj());
		sysDept.setWorkDate(tblDept.getXgsj());
		sysDept.setDeptName(tblDept.getDeptname());
		sysDept.setPid(pidConvert(tblDept));
		sysDept.setIsBigPower(String.valueOf(tblDept.getSfddc()));
		sysDept.setDeptType(tblDept.getDwlx1());
		sysDept.setPowerType(tblDept.getDylx()==""?null:tblDept.getDylx());
		sysDept.setDispatchType(dispatchTypeConvert(tblDept.getDdlx()));
		sysDept.setSorting(sorting);sorting+=10;
		sysDept.setStatisticId(tblDept.getDwbm());
		sysDept.setPersonInCharge(tblDept.getZzr());
		sysDept.setAddress(tblDept.getQyxxdz());
		
		return sysDept;
	}
	
	//产生存储过程
	public void procFactory(List<SysDept> list) {
		
		File file = new File("resource/PROC_INSERT_SYS_DEPT.sql");
		if(!file.exists()) {
			file.delete();
		}
        try {
        	file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        try(
            FileOutputStream fos = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
        	BufferedWriter bw = new BufferedWriter(osw);) {

        	String sql = "create or replace procedure PROC_INSERT_SYS_DEPT is";
        	this.writeLine(bw, sql);
        	sql = "begin";
        	this.writeLine(bw, sql);
        	for(SysDept sysDept : list) {
        		sql = getInsertSql(sysDept);
        		this.writeLine(bw, sql);
    		}
        	sql = "commit;";
        	this.writeLine(bw, sql);
        	sql = "end PROC_INSERT_SYS_DEPT;";
        	this.writeLine(bw, sql);
        } catch (Exception e) {
            e.printStackTrace(); 
        } 
    }
	
	//用特定sql查找pid
	private String pidConvert(TblDept tblDept) {
		String tempSql = null;
		String dwlx1 = tblDept.getDwlx1();
		String deptId = tblDept.getId();
		TblDept parent = null;
		if(dwlx1.equals("区级")) {
			return "-1";
		}
		else if(dwlx1.equals("市级")) {
			tempSql = "select id from tbl_dept where deptname='广西电网公司' and xgzt>0";
			try {
				parent = rq.query(tempSql, new BeanHandler<TblDept>(TblDept.class));
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		else if(dwlx1.equals("市区") || dwlx1.equals("县级")) {
			tempSql = "select id from tbl_dept where xgzt>0 and dwbm=(select subStr(dwbm,0,4) from tbl_dept where id=?)";
			try {
				parent = rq.query(tempSql, new BeanHandler<TblDept>(TblDept.class), new Object[]{deptId});
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		else { //电厂，供电公司，供电局
			tempSql = "select id from tbl_dept where xgzt>0 and dwbm=(select subStr(dwbm,0,7) from tbl_dept where id=?)";
			try {
				parent = rq.query(tempSql, new BeanHandler<TblDept>(TblDept.class), new Object[]{deptId});
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(parent!=null) {
			return parent.getId();
		}
		else {
			return null;
		}
	}
	
	//调度类型的映射
	private String dispatchTypeConvert(String dispatchType) {
		String value = null;
		if(dispatchType==null) {
			return null;
		}
		switch (dispatchType) {
		case "非统调":
			value = "0";
			break;
		case "统调":
			value = "1";
			break;
		default:
			value = null;
			break;
		}
		return value;
	}
	
	//根据dcasp的SysDept对象产生对应的insert sql
	
	
	
	public String getInsertSql(SysDept sysDept) {
		String sqlHeader = "insert into sys_dept (guid, gmt_create, gmt_modified, start_date, "
				+ "dept_name, pid, "
				+ "dept_type, sorting, statistics_id";
		
		
		String sqlTail = ") values("+wrapper(sysDept.getGuid())+","+
				"sysdate, sysdate, sysdate,"+
				wrapper(sysDept.getDeptName())+","+wrapper(sysDept.getPid())+","+
				wrapper(sysDept.getDeptType())+","+sysDept.getSorting()+","+wrapper(sysDept.getStatisticId());
		
		if(sysDept.getIsBigPower()!=null&sysDept.getIsBigPower().equals("1")) {
			sqlHeader+=",is_big_power";
			sqlTail+=","+wrapper(sysDept.getIsBigPower());
		}
		if(sysDept.getPowerType()!=null && sysDept.getPowerType().equals("")==false) {
			sqlHeader+=",power_type";
			sqlTail+=","+wrapper(sysDept.getPowerType());
		}
		if(sysDept.getDispatchType()!=null&&sysDept.getDispatchType().equals("")==false) {
			sqlHeader+=",dispatch_type";
			sqlTail+=","+wrapper(sysDept.getDispatchType());
		}
		if(sysDept.getPersonInCharge()!=null&&sysDept.getPersonInCharge().equals("")==false) {
			sqlHeader+=",person_in_charge";
			sqlTail+=","+wrapper(sysDept.getPersonInCharge());
		}
		if(sysDept.getAddress()!=null&&sysDept.getAddress().equals("")==false) {
			sqlHeader+=",address";
			sqlTail+=","+wrapper(sysDept.getAddress());
		}
		
		String sql = sqlHeader+sqlTail+");";
		return sql;
	}
}
